<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:str="http://exslt.org/strings"
    xmlns:ep="urn:ep"
    extension-element-prefixes="str"
    version="1.0">

    <xsl:import href="ep_render_component_parts.xsl"/>

<!-- render_folder_tree() -->

    <xsl:template name="render_folder_tree">
        <xsl:param name="folder"/>
        <xsl:param name="current_folder_id_input_name"/>
        <xsl:param name="current_root_dataset_id_input_name"/>
        <xsl:param name="view_current_only"/>

        <xsl:variable name="gid" select="generate-id($folder)"/>

        <script type="text/javascript">
            arrFolders['<xsl:value-of select="$gid"/>'] = new Object(); 
        </script>
        <div id="entity_{$gid}" open="true">
            <table border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td>
                        <xsl:call-template name="hierarchy">
                            <xsl:with-param name="this" select="$folder"/>
                        </xsl:call-template>
                    </td>
                    <td width="100%" valign="top">
                        <div id="folder_ac_{$gid}" onmouseover="this.style.backgroundColor='white';this.style.cursor='pointer';" onmouseout="this.style.backgroundColor='#eeeeee'">
                            <xsl:if test="not($view_current_only=1)">
                                <xsl:attribute name="onclick">ep_de_change_current_folder('<xsl:value-of select="$folder/@id"/>')</xsl:attribute>
                            </xsl:if>
                            <xsl:attribute name="style">
                                padding-left: 7px; font-size:10px;white-space:nowrap;<xsl:if test="$folder/@is_current=1">font-weight: bold;</xsl:if>
                            </xsl:attribute>
                            <xsl:value-of select="$folder/@name"/>
                        </div>
                    </td>
                </tr>
            </table>
            <xsl:for-each select="$folder/folder">
                <xsl:call-template name="render_folder_tree">
                    <xsl:with-param name="folder" select="."/>
                    <xsl:with-param name="current_folder_id_input_name" select="$current_folder_id_input_name"/>
                    <xsl:with-param name="current_root_dataset_id_input_name" select="$current_root_dataset_id_input_name"/>
                </xsl:call-template>
            </xsl:for-each>
        </div>
    </xsl:template>

<!-- render_datasets() -->

    <xsl:template name="render_datasets">
        <xsl:param name="datasets"/>
        <xsl:param name="view_current_only"/>

        <xsl:variable name="current_root_dataset_id_input_name">current_root_dataset_id___c_<xsl:value-of select="ancestor::component/@id"/>_s_<xsl:value-of select="ancestor::section/@id"/></xsl:variable>
        <span class="toolSectionTitle">Dataset</span>
        <div>
            <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td style="padding-left: 3px;z-index:0">
                        <select id="current_root_dataset_id" name="current_root_dataset_id" onChange="ep_de_change_current_root_dataset('{@selectable_actions}','{@select_actions_by_epc_id}')" style="width:100%;background-color:#eeeeee;z-index:0">
                            <xsl:for-each select="$datasets/item">
                                <option value="{@key}" style="z-index:0">
                                    <xsl:if test="$view_current_only=1">
                                        <xsl:attribute name="disabled"/>
                                    </xsl:if>
                                    <xsl:if test="@is_current=1">
                                        <xsl:attribute name="selected"/>
                                    </xsl:if>
                                    <xsl:value-of select="text()"/>
                                </option>
                            </xsl:for-each>
                        </select>
                    </td>
                </tr>
            </table>
        </div>
    </xsl:template>

    <xsl:template name="dataset_explorer_node">
        <xsl:param name="uid" />
        <xsl:choose>
            <xsl:when test="count(./*[name()!='meta'])&gt;0"><!-- we have children -->
                <span class="ep_de_node" onclick="ep_toggle_de_node('#node_{$uid}')">
                    <xsl:choose>
                        <!-- TO DO -->
                        <xsl:when test="false() and name()='folder' and not(@is_current='1') and count(ancestor::folder)&gt;0"><img src="{$html_href}/static/images/tree12_closed.png" /></xsl:when>
                        <xsl:otherwise><img src="{$html_href}/static/images/tree12_open.png" /></xsl:otherwise>
                    </xsl:choose>
                </span>
            </xsl:when>
            <xsl:otherwise>
                <span class="ep_de_node" onclick="ep_toggle_de_node('#node_{$uid}')"><img src="{$html_href}/static/images/blank.gif" /></span>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:call-template name="dataset_explorer_item" />
        <xsl:call-template name="dataset_explorer_controls" />
    </xsl:template>

    <xsl:template name="dataset_explorer_item">
        <span class="ep_de_item">
            <xsl:choose>
                <xsl:when test="name()='folder'"><xsl:call-template name="dataset_explorer_folder" /></xsl:when>
                <xsl:when test="name()='dataset'"><xsl:call-template name="dataset_explorer_dataset" /></xsl:when>
                <xsl:when test="name()='action'"><xsl:call-template name="dataset_explorer_action" /></xsl:when>
            </xsl:choose>
        </span>
    </xsl:template>

    <xsl:template name="dataset_explorer_folder">
        <xsl:variable name="name_suffix">
            <xsl:if test="@is_current='1'">_open</xsl:if>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="count(ancestor::folder)=0"><img src="{$html_href}/static/images/folder_home{$name_suffix}.png" /></xsl:when>
            <xsl:otherwise><img src="{$html_href}/static/images/folder{$name_suffix}.png" /></xsl:otherwise>
        </xsl:choose>
        <span style="padding: 1px 3px">
            <xsl:if test="@is_current='1'">
                <xsl:attribute name="class">current</xsl:attribute>
            </xsl:if>
            <a href="javascript:ep_de_change_current_folder({@id});"><xsl:value-of select="@name" /></a>
        </span>
    </xsl:template>

    <xsl:template name="dataset_explorer_action">
        <xsl:choose>
            <xsl:when test="position()=1">
                <xsl:choose>
                    <xsl:when test="meta[@name='upload_data_type']='exp::affy'"><img src="{$html_href}/static/images/affy_data.png" /></xsl:when>
                    <xsl:when test="meta[@name='upload_data_type']='exp::arrayexpress'"><img src="{$html_href}/static/images/ae_data.png" /></xsl:when>
                    <xsl:otherwise><img src="{$html_href}/static/images/data.png" /></xsl:otherwise>
                </xsl:choose>
                <xsl:value-of select="parent::node()/@dataset_name" />
            </xsl:when>
            <xsl:otherwise>
                <img src="{$html_href}/static/images/action.png" /><xsl:value-of select="@component_name" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="dataset_explorer_controls">
        <span class="ep_de_controls" style="">
            <xsl:choose>
                <xsl:when test="name()='folder'"><xsl:call-template name="dataset_explorer_folder_controls" /></xsl:when>
                <xsl:when test="name()='action' and position()=1"><xsl:call-template name="dataset_explorer_dataset_controls" /></xsl:when>
                <xsl:when test="name()='action'"><xsl:call-template name="dataset_explorer_action_controls" /></xsl:when>
            </xsl:choose>
        </span>
    </xsl:template>

    <xsl:template name="dataset_explorer_folder_controls">
    <!--
        <a href="javascript:new_folder({@id})"><img src="{$html_href}/static/images/folder_add.png" /></a>
        <xsl:if test="count(ancestor::folder)&gt;0">
            <a href="javascript:delete_folder({@id})"><img src="{$html_href}/static/images/folder_delete.png" /></a>
            <input id="chk_folder_{@id}" name="chk_folder_{@id}" style="display: none" type="checkbox" />
        </xsl:if>
    -->
    </xsl:template>
<!--

    <xsl:template name="data_explorer_dataset_controls">
        <a href="{parent::node()/@data}" target="_blank"><img src="{$html_href}/static/images/data_view.png" title="View dataset"/></a>
        <a href="{parent::node()/@rows}" target="_blank"><img src="{$html_href}/static/images/data_annots.png" title="View row headers" /></a>
        <a href="{parent::node()/@cols}" target="_blank"><img src="{$html_href}/static/images/data_factors.png" title="View column headers"/></a>
        <a href="javascript:delete_dataset({parent::node()/@dataset_id})"><img src="{$html_href}/static/images/data_delete.png" title="Delete dataset" /></a>
        <input id="chk_dataset_{parent::node()/@dataset_id}" name="chk_dataset_{parent::node()/@dataset_id}" style="display: none" type="checkbox" />
    </xsl:template>


    <xsl:template name="data_explorer_action_controls">
        <a href="{$cgi_href}/display_action_meta.pl?action_id={@action_id}" target="_blank"><img src="{$html_href}/static/images/action_output_view.png" /></a>
        <a href="javascript:saveas_action({@id})"><img src="{$html_href}/static/images/data_save.png" /></a>
        <a href="javascript:delete_action({@action_id})"><img src="{$html_href}/static/images/data_delete.png" /></a>
        <input id="chk_action_{@action_id}" name="chk_action_{@action_id}" style="display: none" type="checkbox" />
    </xsl:template>
-->
    <xsl:template match="/folder_tree//folder">
                <xsl:variable name="uid" select="generate-id(.)" />
                <li id="node_{$uid}">
                    <div id="entity_{$uid}" class="unselected" onmouseover="ep_de_select_entity('#entity_{$uid}')" onmouseout="ep_de_unselect_entity('#entity_{$uid}')">
                        <xsl:call-template name="dataset_explorer_node">
                            <xsl:with-param name="uid" select="$uid" />
                        </xsl:call-template>
                    </div>
                    <xsl:if test="count(child::node())&gt;0"><ol><xsl:apply-templates /></ol></xsl:if>
                </li>
    </xsl:template>



    <!-- ******************* dataset_explorer  ******************* -->
    <xsl:template match="dataset_explorer">
        <xsl:param name="query"/>
        <xsl:param name="target"/>
        <xsl:param name="display_component"/>
        <xsl:param name="epc_input"/>

        <xsl:variable name="folders" select="ep:get_folder_tree()"/>
        <xsl:variable name="datasets" select="ep:dynamic_data(string(ancestor::component/@id), 'list_datasets', string($folders/folder_tree//folder[@is_current=1]/@id))" />
<!--
        <xsl:variable name="current_folder_id_input_name">current_folder_id___c_<xsl:value-of select="ancestor::component/@id"/>_s_<xsl:value-of select="ancestor::section/@id"/></xsl:variable>
        <xsl:variable name="current_root_dataset_id_input_name">current_root_dataset_id___c_<xsl:value-of select="ancestor::component/@id"/>_s_<xsl:value-of select="ancestor::section/@id"/></xsl:variable>
        <xsl:variable name="view_current_only" select="@view_current_only"/>
        <xsl:variable name="select_actions_by_epc_id" select="@select_actions_by_epc_id"/>
        <xsl:variable name="selectable_actions"><xsl:choose><xsl:when test="@selectable_actions>0"><xsl:value-of select="@selectable_actions"/></xsl:when><xsl:otherwise>0</xsl:otherwise></xsl:choose></xsl:variable>
-->
<!--
        <script src="{$html_href}/static/js/ep_dataset_explorer.js" type="text/javascript"></script>
        <script src="{$html_href}/static/js/dtree.js" type="text/javascript"></script>
-->
        <script type="text/javascript">
        function
        ep_toggle_de_node(node)
            {
            var e = window.event;
            if (e &amp;&amp; e.stopPropagation)
                e.stopPropagation();

            var node_img = $(node).find(".ep_de_node > img")[0];
            var img_src = is_msie_6() ? node_img.realSrc : node_img.src;
            var enclosure = $(node).find("ol:first");

            if ( img_src.match(/closed/ig) )
                { // open
                node_img.src = img_src.replace(/closed/ig, "open");
                enclosure.show().end();
                }
            else
                {
                node_img.src = img_src.replace(/open/ig, "closed");
                enclosure.hide().end();
                }
            }

        function
        ep_de_select_entity(entity)
            {
            $(entity).addClass("selected").removeClass("unselected");
            }

        function
        ep_de_unselect_entity(entity)
            {
            $(entity).addClass("unselected").removeClass("selected");
            }

        function
        ep_de_change_current_folder(folder_id)
            {
            document.mainfrm.active_component_id.value = "";
            if ( document.mainfrm.current_folder_id.value == folder_id )
                {
                return;
                }
        
            document.mainfrm.current_folder_id.value = folder_id;
            document.mainfrm.current_root_dataset_id.value = "";
            document.mainfrm.submit();
            }

        function
        ep_de_select_items( select_element_id, select_type )
            {
            var current_action_id = $("#ep_current_action_id").val();
            if (current_action_id)
                {
                var w = window.open
                            ( $("#action_" + current_action_id + "_select" + select_type).text()
                            , "select" + select_type
                            , "height=500,width=250,left=300,top=100,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,resizable=yes"
                            );
                window.items = document.getElementById(select_element_id);
                w.focus();
                }
            }

        function
        ep_de_change_current_action(action_id)
            {
            var current_action_id = $("#ep_current_action_id").val();
//          if (action_id != current_action_id)
                {
                $("#ep_analysis_history").find(".current").removeClass("current");
                $("#action_" + action_id).addClass("current");
                $("#ep_current_action_id").val(action_id).end();
                $("#ep_de_stat_mean").html("Mean: " + $("#action_" + action_id + "_mean").text());
                $("#ep_de_stat_stdev").html("Stdev: " + $("#action_" + action_id + "_stdev").text());
                $("#ep_de_stat_rows").html("Rows: " + $("#action_" + action_id + "_rows").text());
                $("#ep_de_stat_cols").html("Cols: " + $("#action_" + action_id + "_cols").text());
                $("#ep_de_graph_histogram").attr("src", $("#action_" + action_id + "_histogram").text());
                $("#ep_de_graph_legend").attr("src", $("#action_" + action_id + "_legend").text() != "" ? $("#action_" + action_id + "_legend").text() : html_href + "/static/images/blank.gif" );
                // work on factors - TO DO
                var factor_selector_element = $("#factors");
                if ( factor_selector_element )
                    {
                    factor_selector_element.empty();
                    var factors = String($("#action_" + action_id + "_factors").text()).replace(/^\s*|\s*$/g,"").split(";");
                    if ( factors &gt; "")
                        {
                        var options = "";
                        for (f in factors)
                            options += ("&lt;option value=\"" + f + "\"&gt;" + String(factors[f]).replace(/^\s*|\s*$/g,"") + "&lt;/option&gt;");

                        factor_selector_element.append(options);
                        }
                    }
                $.get( cgi_href + "/ep_dataset_explorer_helper.pl", { op: "change_current_action", session_id: session_id, action_id: action_id } );
                }
            }

        function
        ep_de_clear_action_stats()
            {
            $("#ep_de_stat_mean").html("Mean: -");
            $("#ep_de_stat_stdev").html("Stdev: -");
            $("#ep_de_stat_rows").html("Rows: -");
            $("#ep_de_stat_cols").html("Cols: -");
            $("#ep_de_graph_histogram").attr("src", html_href + "/static/images/blank.gif");
            $("#ep_de_graph_legend").attr("src", html_href + "/static/images/blank.gif");
            $("#factors").empty();
            }

        function
        ep_de_change_current_root_dataset()
            {
            var dataset_id = $("#current_root_dataset_id").val();
            if (dataset_id > "")
                {
                $("#ep_analysis_history").html("Please wait while history is being pulled up from the server...");
                $.get
                    ( cgi_href + "/ep_dataset_explorer_helper.pl"
                    , { op: "change_current_root_dataset", session_id: session_id, dataset_id: dataset_id }
                    , function(history_html)
                        {
                        if (history_html)
                            {
                            $("#ep_analysis_history").html(history_html).end();
                            var current_action_id = String($("#ep_analysis_history").find(".current")[0].id).replace(/action_/ig, "");
                            if ( current_action_id > 0 )
                                ep_de_change_current_action(current_action_id);
                            else
                                ep_de_clear_action_stats();
                            }
                        else
                            {
                            $("#ep_analysis_history").html("No history available for this dataset.");
                            ep_de_clear_action_stats();
                            }
                        }
                    );
                }
            else
                {
                // nothing has been selected
                $("#ep_analysis_history").html("");
                $("#current_root_dataset_id").attr("disabled", true);
                ep_de_clear_action_stats();
                }
            }

        // global code
        $( function()
            {
            ep_de_change_current_root_dataset();
            }
        );
        </script>

        <input type="hidden" id="ep_current_folder_id" name="current_folder_id" value="{$folders//folder[@is_current=1]/@id}" />
        <input type="hidden" id="ep_current_action_id" name="current_action_id" value="0" />
        <div id="ep_dataset_explorer" style="position: relative; top: 0px; left: 0px; width: 100%; height: 600px; padding: 0; border: 0px">
            <div style="margin-left: 150px; padding: 0; height: 200px">
                <div style="padding-top: 0px; padding-bottom: 0px; height: 30px">
                    <select id="current_root_dataset_id" name="current_root_dataset_id" onChange="ep_de_change_current_root_dataset()" style="z-index: 0; width:100%">
                        <xsl:for-each select="$datasets/item">
                            <option value="{@key}" style="z-index:0">
                                <xsl:if test="@is_current=1">
                                    <xsl:attribute name="selected"/>
                                </xsl:if>
                                <xsl:value-of select="text()"/>
                            </option>
                        </xsl:for-each>
                    </select>
                </div>
                <div id="ep_analysis_history" style="position: relative; overflow: auto; height: 170px; font-family: 'Lucida Grande', Verdana, Arial; font-size: 11px; color: #808080; border: solid 1px #dfdfdf" />
            </div>
            <div style="position: absolute; top: 0px; left: 0px; width: 145px; height: 200px; padding: 0">
                <div style="position: relative; height: 200px; overflow: auto; border: solid 1px #dfdfdf">
                    <ol><xsl:apply-templates select="$folders/folder_tree/folder" /></ol>
                </div>
            </div>
            <div style="position: absolute; top: 210px; left: 0px; width: 100%; height: 370px; padding: 0">
                <div id="ep_de_stats" style="margin-bottom: 6px">
                    <div id="ep_de_stat_mean" class="ep_de_stat_value">Mean: -</div>
                    <div id="ep_de_stat_stdev" class="ep_de_stat_value">Stdev: -</div>
                    <div id="ep_de_stat_rows" class="ep_de_stat_value">Rows: -</div>
                    <div id="ep_de_stat_cols" class="ep_de_stat_value">Cols: -</div>
                </div>
                <div id="ep_de_graphics" style="position: relative; height: 350px">
                    <div style="position: absolute; left: 0px; top: 0px; width: 500px; height: 350px">
                        <img id="ep_de_graph_histogram" src="{$html_href}/static/images/blank.gif" width="500" height="350" />
                    </div>
                    <div style="position: absolute; left: 510px; top: 0px; width: 250px; height: 350px">
                        <img id="ep_de_graph_legend" src="{$html_href}/static/images/blank.gif" width="250" height="350" />
                    </div>
                </div>
            </div>
        </div>

    </xsl:template>
</xsl:stylesheet>
